package com.ebelter.bodyfatscale.ui.activity;

import android.view.View;
import android.widget.EditText;

import com.ebelter.bodyfatscale.R;
import com.ebelter.bodyfatscale.common.Constants;
import com.ebelter.bodyfatscale.moudules.db.Daos;
import com.ebelter.bodyfatscale.moudules.db.beans.UsersData;
import com.ebelter.bodyfatscale.moudules.https.NetUtils;
import com.ebelter.bodyfatscale.util.ToastUtil;
import com.ebelter.bodyfatscale.util.UserSpUtil;

import butterknife.BindView;
import butterknife.OnClick;

public class ModifyActivity extends BaseActivity {

    public static final String DISPLAY_VALUE = "DISPLAY_VALUE";
    public static final String MODIFY_STYLE = "MODIFY_STYLE";
    public static final int MODIFY_NAME = 1;
    public static final int MODIFY_EMAIL = 2;
    private static final String TAG = "ModifyActivity";
    public int CURRENT_MODIFY_STYLE = 0;

    private String displayValue;

    @BindView(R.id.modify_ed)
    EditText modify_ed;

    private NetUtils myNetUtils;
    @Override
    protected int loadLayoutById() {
        return R.layout.activity_modify;
    }

    @Override
    protected void initViews() {

    }

    @Override
    protected void initData() {
        CURRENT_MODIFY_STYLE = getIntent().getIntExtra(MODIFY_STYLE,0);
        displayValue = getIntent().getStringExtra(DISPLAY_VALUE);
        if (CURRENT_MODIFY_STYLE==0){
            finish();
        }else if (CURRENT_MODIFY_STYLE==MODIFY_NAME){

            modify_ed.setHint(R.string.nike_name);
            modify_ed.setMaxEms(20);

        }else if (CURRENT_MODIFY_STYLE==MODIFY_EMAIL){
            modify_ed.setHint(R.string.email);

        }

        myNetUtils = NetUtils.getInstance();

        modify_ed.setText(displayValue);

    }


    @OnClick({ R.id.modify_back_bt,R.id.modify_done_tv})
    public void btClick(View view){
        switch (view.getId()){
            case R.id.modify_back_bt:
                finish();
                break;
            case R.id.modify_done_tv:

                String modifyStr=modify_ed.getText().toString().trim();

                if (modifyStr.length()>20){
                    ToastUtil.show(R.string.nikename_over_tip);
                    return;
                }



                long userid = UserSpUtil.readLong(Constants.IUser.USER_ID);
                UsersData usersData = Daos.getInstance().getUsersDataFromUserId(userid);
                usersData.name=modifyStr;
                usersData.update();
                ToastUtil.show(R.string.Modify_Success);
                UserSpUtil.writeString(Constants.IUser.NICK_NAME, modifyStr);
                displayValue=modifyStr;




                break;


        }
    }


}
